<?php
namespace Asset\Controller;
use Common\Controller\AdminbaseController;
class RetainedController extends AdminbaseController {
	protected $retained_users_model,$retained_device_model,$retained_users_detailed_model,$retained_device_detailed_model,$real_users_model,$store_model,$device_model;
	function _initialize() {
		parent::_initialize();
		$this->retained_users_model = D("RetainedUsers");
		$this->retained_device_model = D("RetainedDevice");
		$this->retained_users_detailed_model = D("RetainedUsersDetailed");
		$this->retained_device_detailed_model = D("RetainedDeviceDetailed");
        $this->real_users_model = D("RealUsers");
		$this->store_model = D("Store");
		$this->device_model = D("Device");
	}

    private function devicestatus() {
		$devicestatus =array(
	        array(
				'100'=>array('id'=>'100','name'=>'电影院'),
				'105'=>array('id'=>'105','name'=>'私人影吧'),
				'110'=>array('id'=>'110','name'=>'奶茶/水吧'),
				'120'=>array('id'=>'120','name'=>'商超'),
				'125'=>array('id'=>'125','name'=>'KTV'),
				'130'=>array('id'=>'130','name'=>'便利店'),
				'135'=>array('id'=>'135','name'=>'烘培蛋糕店'),
				'140'=>array('id'=>'140','name'=>'西餐厅/中餐厅'),
				'150'=>array('id'=>'150','name'=>'咖啡/甜品店'),
				'160'=>array('id'=>'160','name'=>'酒吧/夜店'),
				'170'=>array('id'=>'170','name'=>'小吃店/零食店'),
				'180'=>array('id'=>'180','name'=>'网咖/网吧'),
				'190'=>array('id'=>'190','name'=>'主题公园/景区'),
				'200'=>array('id'=>'200','name'=>'副食品店'),
				'500'=>array('id'=>'500','name'=>'其他'),
			),
	        array(
	            '0'=>array('id' => '0', 'name'=>'请选择'),
	            '1'=>array('id' => '1', 'name'=>'停用'),
	            '2'=>array('id' => '2', 'name'=>'激活'),
	            '3'=>array('id' => '3', 'name'=>'损坏'),
	            '4'=>array('id' => '4', 'name'=>'发货'),
	            '5'=>array('id' => '5', 'name'=>'入库'),
	            '6'=>array('id' => '6', 'name'=>'出场'),
	        ),
			);
		return $devicestatus;
    }
    function users() {
		$count=$this->retained_users_model
		->where($search)
		->count();
		$page = $this->page($count, 50);
		$retained = $this->retained_users_model
		->where($search)
		->limit($page->firstRow . ',' . $page->listRows)
		->order("id DESC")
		->select();
		$this->assign("retained", $retained);
		$this->assign("page", $page->show('Admin'));
		$this->assign("count",$count);
		$this->display();
    }

    function devices() {
		$count=$this->retained_device_model
		->where($search)
		->count();
		$page = $this->page($count, 50);
		$retained = $this->retained_device_model
		->where($search)
		->limit($page->firstRow . ',' . $page->listRows)
		->order("id DESC")
		->select();
		$this->assign("retained", $retained);
		$this->assign("page", $page->show('Admin'));
		$this->assign("count",$count);
		$this->display();
    }

    function user_detail() {
    	if(I('get.id')){
			$detailed = $this->retained_users_detailed_model->where(array('id'=>I('get.id')))->find();
			$where[] = array('weixin_id'=>array('in',$detailed[I('get.day')]));
			if(!empty($detailed[I('get.day')])){
				$count = $this->real_users_model->where($where)->count();
				$page = $this->page($count, 50);
				$users = $this->real_users_model
				->where($where)
				->limit($page->firstRow . ',' . $page->listRows)
				->order("id DESC")
				->select();
				$this->assign("users", $users);
				$this->assign("page", $page->show('Admin'));
				$this->assign("count",$count);
    		}
		}
		$this->display();
    }
    function devices_detail() {
    	if(I('get.id')){
			$detailed = $this->retained_device_detailed_model->where(array('id'=>I('get.id')))->find();
			if(!empty($detailed[I('get.day')])){
				$count = $this->device_model->where(array('devid'=>array('in',$detailed[I('get.day')])))->count();
				$page = $this->page($count, 50);
				$devices = $this->device_model
				->where(array('devid'=>array('in',$detailed[I('get.day')])))
				->limit($page->firstRow . ',' . $page->listRows)
				->order("id DESC")
				->select();
				if($devices){
					foreach($devices as $device){
						if($device['store_id']){
							$store_id[] = $device['store_id'];
						}
					}
					if($store_id){
						$where[] = array('store_id'=>array('in',$store_id));
						$where[] = array('title'=>array('neq',""));
						$stores=$this->store_model->where($where)->select();
						$this->assign("stores", $stores);
					}
				}
				$dstatus = $this->devicestatus();
				$this->assign("bstatus", $dstatus[1]);
				$this->assign("stype", $dstatus[0]);
				$this->assign("devices", $devices);
				$this->assign("page", $page->show('Admin'));
				$this->assign("count",$count);
			}
    	}
		$this->display();
    }

}
